package summer.hot100.v1;

public class Solution188 {
    public int maxProfit(int k, int[] prices) {
        int m = prices.length;
        int[][] dp = new int[m][2 * k + 1];
        for (int i = 1; i < 2 * k + 1; i += 2) {
            dp[0][i] = -prices[0];
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < 2 * k + 1; j++) {
                if (j % 2 == 1) dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1] - prices[i]);
                else dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1] + prices[i]);
            }
        }
        return dp[m - 1][2 * k];
    }
}
